package com.itheima.imclient100;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.view.menu.MenuBuilder;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.ashokvarma.bottomnavigation.TextBadgeItem;
import com.hyphenate.chat.EMClient;
import com.itheima.imclient100.utils.FragmentFactory;
import com.itheima.imclient100.view.AddFriendActivity;
import com.itheima.imclient100.view.BaseFragment;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MainActivity extends BaseActivity {

    @InjectView(R.id.tv_title)
    TextView tvTitle;
    @InjectView(R.id.tb_toolbar)
    Toolbar tbToolbar;
    @InjectView(R.id.fl_container)
    FrameLayout flContainer;
    @InjectView(R.id.bottom_navigation_bar)
    BottomNavigationBar bottomNavigationBar;
    @InjectView(R.id.activity_main)
    LinearLayout activityMain;
    private FragmentManager supportFragmentManager;
    private String[] titles = {"消息","联系人","动态"};
    private TextBadgeItem numberBadgeItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        supportFragmentManager = getSupportFragmentManager();
        initBottomBar();
        initTitleBar();


        FragmentTransaction transaction = supportFragmentManager.beginTransaction();
        List<Fragment> fragments = supportFragmentManager.getFragments();
        if(fragments != null && fragments.size()>0){
            for(Fragment fragment:fragments){
                transaction.remove(fragment);
            }
            transaction.commit();
        }

        transaction = supportFragmentManager.beginTransaction();
        BaseFragment fragment = FragmentFactory.getFragment(0);
        transaction.add(R.id.fl_container,fragment);
        transaction.commit();

        setNumberBadgeItem(EMClient.getInstance().chatManager().getUnreadMessageCount());
    }

    private void initTitleBar() {
        tbToolbar.setTitle("");
        setSupportActionBar(tbToolbar);
        //设置显示标题栏左侧的返回按钮
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_main,menu);
        //MenuBuilder 通过这个对象可以控制菜单项中图标的显示和隐藏
        MenuBuilder builder = (MenuBuilder) menu;
        builder.setOptionalIconsVisible(true);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId){
            case R.id.menu_item_addfriend:
                startActivity(new Intent(getApplicationContext(), AddFriendActivity.class));
                break;
            case R.id.menu_item_scan:
                showToast("扫一扫");
                break;
            case R.id.menu_item_about:
                showToast("关于");
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initBottomBar() {
        BottomNavigationItem messageItem = new BottomNavigationItem(R.mipmap.conversation_selected_2, "消息");
        //底部导航选项右上角的气泡文字
        numberBadgeItem = new TextBadgeItem();
        //给消息这个选项设置气泡
        messageItem.setBadgeItem(numberBadgeItem);
        //隐藏气泡
        numberBadgeItem.hide();
        BottomNavigationItem contactItem = new BottomNavigationItem(R.mipmap.contact_selected_2, "联系人");
        BottomNavigationItem pluginItem = new BottomNavigationItem(R.mipmap.plugin_selected_2, "动态");

        bottomNavigationBar.addItem(messageItem)
                            .addItem(contactItem)
                            .addItem(pluginItem)
                            .setActiveColor(R.color.btn_normal)//设置被选中之后的tab颜色
//                            .setInActiveColor() 设置没被选中的tab颜色
                            .initialise();

        bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                System.out.println("onTabSelected 选中"+position);
                FragmentTransaction transaction = supportFragmentManager.beginTransaction();
                BaseFragment fragment = FragmentFactory.getFragment(position);
                if(fragment.isAdded()){
                    //把已经添加进来的fragment显示出来
                    transaction.show(fragment);
                }else{
                    //添加fragment
                    transaction.add(R.id.fl_container,fragment);
                }
                transaction.commit();
                tvTitle.setText(titles[position]);
            }

            @Override
            public void onTabUnselected(int position) {
                System.out.println("onTabUnselected 没被选中"+position);
                FragmentTransaction transaction = supportFragmentManager.beginTransaction();
                BaseFragment fragment = FragmentFactory.getFragment(position);
                //隐藏已经添加进来的fragment
                transaction.hide(fragment);
                transaction.commit();
            }

            @Override
            public void onTabReselected(int position) {
                System.out.println("onTabReselected 重新选中"+position);


            }
        });
    }

    public void setNumberBadgeItem(int unreadCount){
        //如果未读数量是0 隐藏右上角的气泡
        if(unreadCount == 0){
            numberBadgeItem.hide();
        }else if(unreadCount >99){
            //如果未读数量是>99 显示右上角的气泡 显示99
            numberBadgeItem.setText("99");
            numberBadgeItem.show();
        }else{
            numberBadgeItem.setText(unreadCount+"");
            numberBadgeItem.show();
        }
    }
}
